<template>
  <div>
    <div class="block">
    <span class="demonstration">时间区间：</span>
    <el-date-picker
      v-model="createTime"
      type="daterange"
      value-format="yyyy-MM-dd HH:mm:ss"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
    <span class="demonstration">状态：</span>
    <el-select v-model="status" clearable placeholder="全部"  change="isverifychange" class="filter-item" style="width: 130px; margin-right: 8px;">
      <el-option
            v-for="item in isverifylist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button    @click="toQuery()"  type="primary" plain>查询</el-button>
     <el-select v-model="exporttype" clearable placeholder="导出全部" class="filter-item" style="width: 130px; margin-right: 8px;">
          <el-option
            v-for="item in handleOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
     <el-button    @click="handleExportOption()"  type="primary" plain>导出</el-button>
  </div>
  <el-table
      :data="data"
      border
      show-summary
      :summary-method="totalOutPrice"
      style="width: 100%">

        
<el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column
      label="序号"
     type="index"
      width="50">
    </el-table-column>
      <el-table-column
        label="下单时间"
     >
     <template slot-scope="scope">
            <span v-if="scope.row.payTime != null">
            	{{ parseTime(scope.row.payTime, "{y}-{m}-{d} {h}:{i}:{s}") }}
            </span>
          </template>
      </el-table-column>
      <el-table-column
        prop="cateName"
        label="类别"
      >
      </el-table-column>
      <el-table-column
        prop="storeName"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="totalNum"
        sortable
        label="购买数量">
      </el-table-column>
      <el-table-column
        prop="payPrice"
        label="单价（元）">
      </el-table-column>
      <el-table-column
        prop="totalPrice"
        sortable
        label="总金额">
      </el-table-column>
      <el-table-column
        prop="payTypeName"
        label="支付方式">
      </el-table-column>
      <el-table-column
        prop="verifyCode"
        label="核销码">
      </el-table-column>
      <!-- <el-table-column
      prop="statusName"
      label="是否核销">
      </el-table-column>
      <el-table-column
      prop="status"
      label="状态">
      </el-table-column> -->
      <el-table-column
      label="状态">
      <template slot-scope="scope">
        <span v-if="scope.row.statusName =='未核销'">未核销</span>
        <span v-else-if="scope.row.status =='-1'">申请退款</span>
        <span v-else>已核销</span>
      </template>
      </el-table-column>
      <el-table-column
      prop="verifgTime"
        label="核销时间">
      </el-table-column>

      <el-table-column
      prop="verifgRealname"
        label="核销员名字">
      </el-table-column>
    </el-table>
    <el-footer class="footer-contains">
      <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </el-footer>
  </div>
  </template>

  <script>
  import initData from '@/mixins/crud'

  import { getOrderlistservice,gettotalservice,downexcelservice,getOrderlistdata } from '@/api/yxStoreOrder'
  import { parseTime, downloadFile } from '@/utils/index'

    export default {
      mixins: [initData],

      data() {
        return {
          tableData: [{}],
          exportdata: [{}],
          createTime:[],
          tablesum:[],
          total:100,
          page: 1,     
          size:10,
           exporttype:'',
           status:'',
          handleOptions: [
            { label: '导出当前页', value: '1' },
            { label: '导出全部', value: '2' }
          ],
          isverifylist:[
            {label: '全部' },          
            {label: '未核销', value: '0'},
            {label: '已核销', value: '2'}           
          ]
         
        }
      },


    created() {
    this.$nextTick(() => {
      this.init()
    })
  },

      methods: {
 
            //表尾合计自定义方法
            totalOutPrice(param) {
                const { columns, data } = param
                const sums = []
                columns.forEach((column, index) => {
                  if (index === 0) {
                    sums[index] = '合计';
                    return;
                  }
                  const values = data.map(item => Number(item[column.property]));
                  if (column.property == 'totalPrice'||column.property == 'payPrice'||column.property == 'totalNum') {
                        sums[index] = values.reduce((prev, curr) => {
                      const value = Number(curr);
                      if (!isNaN(value)) {
                        return prev + curr;
                      } else {
                        return prev;
                      }
                    }, 0);
                    if(column.property == 'totalNum'){
                      sums[index]
                    }else{
                    sums[index]= sums[index].toFixed(2)+'元'; }
                  }
                });
                this.tablesum=sums
                return sums 
              },
    
        async  handleExportOption(){
         
            console.log('导出',)
            if(this.exporttype==1){
              this.exportdata[0].createTime=this.createTime
              this.exportdata[0].status=this.status==2?'已核销':'未核销'//已核销/
          
            for(let i=0;i<this.data.length;i++){
              let exdata={
                payTime:parseTime(this.data[i].payTime, "{y}-{m}-{d} {h}:{i}:{s}"),
                cateName:this.data[i].cateName,
                storeName:this.data[i].storeName,
                totalNum:this.data[i].totalNum,
                payPrice:this.data[i].payPrice,
                totalPrice:this.data[i].totalPrice,
                payTypeName:this.data[i].payTypeName,
                verifyCode:this.data[i].verifyCode,
                status:this.data[i].status,
                verifgTime:this.data[i].verifgTime,
                verifgRealname:this.data[i].verifgRealname
              }
              this.exportdata.push(exdata)
              }    
              let adddata1={
              payTime:'合计',
              totalNum:this.tablesum[5],
              payPrice:Number(this.tablesum[6].slice(0,-1)),
              totalPrice:Number(this.tablesum[7].slice(0,-1) )
              }          
            this.exportdata.push(adddata1)
           }else{     
      // 重置查询状态
      this.exportdata[0].createTime=null
      // this.exportdata[0].createTime[0]='2025-01-01'
      // this.exportdata[0].createTime[1]='今'
      this.exportdata[0].status=='全部'//已核销/
      // 查询数据
     await getOrderlistdata().then (res => {     
      //  查询体
      console.log('导出全部',res.content)
      let totalNumber=0;let payPricenum=0;let totalPricenum=0;
            for(let i=0;i<res.content.length;i++){
              let exdata={
                payTime:parseTime(res.content[i].payTime, "{y}-{m}-{d} {h}:{i}:{s}"),
                cateName:res.content[i].cateName,
                storeName:res.content[i].storeName,
                totalNum:res.content[i].totalNum,
                payPrice:res.content[i].payPrice,
                totalPrice:res.content[i].totalPrice,
                payTypeName:res.content[i].payTypeName,
                verifyCode:res.content[i].verifyCode,
                status:res.content[i].status,
                verifgTime:res.content[i].verifgTime,
                verifgRealname:res.content[i].verifgRealname               
              } 
              totalNumber=res.content[i].totalNum+totalNumber
              payPricenum=res.content[i].payPrice+payPricenum
              totalPricenum=res.content[i].totalPrice+totalPricenum
              this.exportdata.push(exdata)
              }         
      // 表尾  
           let adddata1={
              payTime:'合计',
              totalNum:totalNumber,
              payPrice:payPricenum,
              totalPrice:totalPricenum
              }          
            this.exportdata.push(adddata1)     
          }) 
            }
          downexcelservice(this.exportdata).then(result =>{
          //  downloadFile(result, , 'xlsx')
          const obj = result
          const name ='数据'
          const suffix='xlsx'
          const url = window.URL.createObjectURL(new Blob([obj]))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
          link.setAttribute('download', fileName)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          })
          this.exportdata=[{}]
          },

        beforeInit() {
        this.url = 'api/getOrderdatalist'
        const sort = 'id,desc'
        this.params = {
          page: this.page,
          size: this.size,
          createTime: this.createTime,  
          status:this.status,
        }
        return true
      },
 
      },

    }
  </script>